package cn.ztuo.bitrade.service;

import cn.ztuo.bitrade.dao.CesspitRecordDao;
import cn.ztuo.bitrade.entity.CesspitRecord;
import cn.ztuo.bitrade.entity.Member;
import cn.ztuo.bitrade.entity.QCesspitRecord;
import cn.ztuo.bitrade.service.Base.BaseService;
import cn.ztuo.bitrade.util.PredicateUtils;
import cn.ztuo.bitrade.vo.CesspitRecordVO;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.BooleanExpression;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class CesspitRecordService extends BaseService {
    @Autowired
    private CesspitRecordDao cesspitRecordDao;
    @Autowired
    private MemberService memberService;


    public CesspitRecord save(CesspitRecord cesspitRecord) {
        return cesspitRecordDao.save(cesspitRecord);
    }

    public List<CesspitRecord> findAll() {
        return cesspitRecordDao.findAll();
    }

    public CesspitRecord findById(Long id) {
        return cesspitRecordDao.findById(id);
    }

    public Page<CesspitRecord> getPage(Integer pageNum, Integer pageSize, String coin, Long userId) {
        ArrayList<BooleanExpression> booleanExpressions = new ArrayList<>();
        if (StringUtils.isNotEmpty(coin)) {
            booleanExpressions.add(QCesspitRecord.cesspitRecord.coin.eq(coin.toUpperCase()));
        }
        booleanExpressions.add(QCesspitRecord.cesspitRecord.memberId.eq(userId));
        //只展示进行中/完成/赎回中的记录
        booleanExpressions.add(QCesspitRecord.cesspitRecord.status.in(1,2,3));
        Predicate predicate = PredicateUtils.getPredicate(booleanExpressions);
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = new PageRequest(pageNum - 1, pageSize, sort);
        return cesspitRecordDao.findAll(predicate, pageable);
    }

    public Page<CesspitRecord> getAllPage(CesspitRecordVO cess) {
        ArrayList<BooleanExpression> booleanExpressions = new ArrayList<>();
        if (StringUtils.isNotEmpty(cess.getCoin())) {
            booleanExpressions.add(QCesspitRecord.cesspitRecord.coin.eq(cess.getCoin().toUpperCase()));
        }
        if (cess.getMemberId() != null) {
            Member byPhone = memberService.findOne(cess.getMemberId());
            if(byPhone != null){
                cess.setAccount(byPhone.getId().toString());
            }
            booleanExpressions.add(QCesspitRecord.cesspitRecord.memberId.like("%" + cess.getAccount() + "%")
                    .or(QCesspitRecord.cesspitRecord.userName.like("%" + cess.getAccount() + "%"))
            );
        }
        if (cess.getStatus() == 1 || cess.getStatus() == 2 ||cess.getStatus() == 3) {
            booleanExpressions.add(QCesspitRecord.cesspitRecord.status.eq(cess.getStatus()));
        }else{
            booleanExpressions.add(QCesspitRecord.cesspitRecord.status.in(1,2,3));
        }
        Predicate predicate = PredicateUtils.getPredicate(booleanExpressions);
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = new PageRequest(cess.getPageNum() - 1, cess.getPageSize(), sort);
        return cesspitRecordDao.findAll(predicate, pageable);
    }

    /**
     * 查询用户多个状态的矿机
     * @param id
     * @param status
     * @return
     */
    public List<CesspitRecord> findByMemberIdAndStatusIn(Long id,List<Long> status){
        return cesspitRecordDao.findByMemberIdAndStatusIn(id,status);
    }

    /**
     * 查询用户一个状态的矿机
     * @param id
     * @param status
     * @return
     */
    public CesspitRecord findByMemberIdAndStatus(Long id,Long status){
        return cesspitRecordDao.findByMemberIdAndStatus(id,status);
    }


    public String[] getTotalHashRateP(){
        return cesspitRecordDao.getTotalHashRate();
    }

    public String[] getLeftHashRate(Long id)
    {
        return cesspitRecordDao.getLeftHashRate(id);
    }


    public String[] getRightHashRate(Long id)
    {
        return cesspitRecordDao.getRightHashRate(id);
    }


    /**
     * 统计所有用户的算力综合
     * sum统计所有用户质押矿机的算力总和
     */
    public String[] sumHashRate(){
        return cesspitRecordDao.sumHashRate();
    }

    public String[] sumAmount(long id)
    {
        return cesspitRecordDao.sumAmount(id);
    }



    /**
     * 获取当前用户算力值，没有购买矿机返回0
     * @param id
     */
    public BigDecimal getHashrate(Long id){
         CesspitRecord cesspitRecord  = this.findByMemberIdAndStatus(id,1L);
         if(cesspitRecord==null) return BigDecimal.ZERO;
         return cesspitRecord.getHashrate();
    }

    /**
     * 获取算力排行榜
     * @return
     */
    public List<CesspitRecord> DongtaiAllHashrate(){
        return cesspitRecordDao.DONGTAIHashrate();
    }


}
